<template>
  <div class="bar-chart-container">
    <div ref="chart" class="bar-chart" :style="{ height: height }"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  props: {
    data: {
      type: Array,
      required: true,
      validator: value => value.every(item => 'name' in item && 'value' in item)
    },
    title: {
      type: String,
      default: ''
    },
    height: {
      type: String,
      default: '300px'
    },
    color: {
      type: String,
      default: '#4CAF50'
    }
  },

  data() {
    return {
      chart: null
    }
  },

  watch: {
    data: {
      deep: true,
      handler() {
        this.updateChart();
      }
    }
  },

  mounted() {
    this.initChart();
    window.addEventListener('resize', this.handleResize);
  },

  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
    if (this.chart) {
      this.chart.dispose();
    }
  },

  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.chart);
      this.updateChart();
    },

    updateChart() {
      const option = {
        title: {
          text: this.title,
          left: 'center',
          textStyle: {
            color: '#2c3e50',
            fontSize: 14
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: params => {
            return `${params[0].data.name}<br/>数值: ${params[0].data.value}`;
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: this.data.map(item => item.name),
          axisLine: {
            lineStyle: {
              color: '#95a5a6'
            }
          },
          axisLabel: {
            color: '#7f8c8d',
            rotate: 45
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            lineStyle: {
              color: '#95a5a6'
            }
          },
          axisLabel: {
            color: '#7f8c8d'
          },
          splitLine: {
            lineStyle: {
              color: '#ecf0f1'
            }
          }
        },
        series: [{
          data: this.data,
          type: 'bar',
          barWidth: '60%',
          itemStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: this.color },
              { offset: 1, color: this.color + '80' }
            ]),
            borderRadius: [4, 4, 0, 0]
          },
          label: {
            show: true,
            position: 'top',
            color: '#2c3e50'
          }
        }]
      };

      this.chart.setOption(option);
    },

    handleResize() {
      if (this.chart) {
        this.chart.resize();
      }
    }
  }
}
</script>

<style scoped>
.bar-chart-container {
  width: 100%;
}

.bar-chart {
  width: 100%;
  min-height: 200px;
}
</style>